package com.kdtech.analyse.Bbs.common;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
/**
 * Discuz! 6.0.0 版本
<meta name="generator" content="Discuz! 6.0.0" />
<meta name="author" content="Discuz! Team and Comsenz UI Team" />

 * 0000
 * http://www.xxx.xxx/ XX论坛
 * @author KK
 */
public class BbsAnalyseDiscuzV6 implements AnalyseNews {

	String[] regex = {
			"viewthread.php\\?extra=.*&page=1&tid=[0-9]*",
			"viewthread.php\\?extra=.*&tid=[0-9]*",
			"viewthread.php\\?tid=[0-9]*&extra=page%3D1",
			"viewthread.php\\?tid=[0-9]*",
			"thread-[0-9]*-1-1.html",
			"forum.php\\?extra=.*&mod=viewthread&tid=[0-9]*",
			"[0-9]+.html",
	};

	
	public boolean isDetailPage(String url) {
		url = UrlArgumentTop.FromatUrl(url);
		String before = StringUtils.substringBeforeLast(url, "/");
		if(before.contains("?")){
			return false;
		}

		url = StringUtils.substringAfterLast(url, "/");
		return RegexUtils.matchAny(url, regex);
	}

	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
			return null;
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;
		String updateUrl = url;
		Document doc = Jsoup.parse(htmltxt);
		doc.select("a:containsOwn(登录/注册后可看大图)").remove();
		String author=null;
		Elements select5 = doc.select("div.box.message:contains(本版块只有特定用户组可以访问)");
		if(select5.size()>0){
			return null;
		}
		Elements select = doc.select("div.topic_title_l h1");
		if(select!=null && select.size()>0){
			title = select.get(0).ownText();
		}
		if(StringUtils.isBlank(title)){
			title = doc.select("title").text();
			if (StringUtils.isNotEmpty(title)){
				title = StringUtils.substringBefore(title, " - ");
			}
		}

		Element firstDiv = doc.select("div.viewthread").first();
		if (firstDiv!=null){
			date = DateUtils.matchDate(firstDiv.select("div.postinfo").text());
			author = firstDiv.select("td.postauthor>cite>a.dropmenu").text();
			content = HtmlCleaner.getContentHtml(url,firstDiv.select("div.t_msgfont"));
			String commTxt = firstDiv.select("p:containsOwn(点击量:)").text();
			if (commTxt!=null){
				clickNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "点击量:"));
				commnetNum = 0;
//				commnetNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "回复:"));
			}
		}

		if(date==null){
			Elements select2 = doc.select("div.menber ul");
			if(select2!=null && select2.size()>0){
				date = DateUtils.matchDate(select2.text());
				String commTxt = select2.select("li:containsOwn(回复：)").text();
				commnetNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "回复："));
				String clickTxt = select2.select("li:containsOwn(阅读：)").text();
				clickNum = NumberUtils.matchNumber(StringUtils.substringAfter(clickTxt, "阅读："));
				Elements select3 = doc.select(".topic_c");
				select3.select("div.topic_nologin,a:contains(登录/注册后可查看大图)").remove();
				if(select3!=null && select3.size()>0){
					content = HtmlCleaner.getContentHtml(url,select3);
				}
				Elements select4 = doc.select("div.topic_name");
				if(select4!=null && select4.size()>0){
					author = select4.get(0).text();
				}
			}
			if(date==null){
				date = DateUtils.matchDate(doc.select("div.x_list div.r div.sj").text());
				if(StringUtils.isBlank(author)){
					Element first = doc.select("html body div.x_list div.l a.cBlack").first();
					if(first!=null){
						author = first.text();
					}
				}
			}
		}
		if(StringUtils.isBlank(content)){
			 Element first = doc.select("div.x_list div.r div.nr").first();
			 content = HtmlCleaner.getContentHtml(url,first);
		}

			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(4);
			news.setAuthor(author);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(commnetNum);
			news.setClickNum(clickNum);
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setUpdateUrl(updateUrl);
			return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	

	

}
